﻿using System;

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Xml;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace EVDashboard
{
    class Util
    {
        //private byte[] m_soundBytes;
        // private string m_fileName;

        private enum Flags
        {
            SND_SYNC = 0x0000,  /* play synchronously (default) */
            SND_ASYNC = 0x0001,  /* play asynchronously */
            SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
            SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
            SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
            SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            SND_ALIAS = 0x00010000, /* name is a registry alias */
            SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
            SND_FILENAME = 0x00020000, /* name is file name */
            SND_RESOURCE = 0x00040004  /* name is resource name or atom */
        }

        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);

        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);

        public static string GetStorageCard()
        {
            //initialize the path as an empty string
            string firstCard = "";

            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo("\\");
            System.IO.FileSystemInfo[] fsi = di.GetFileSystemInfos();

            //iterate through them
            for (int x = 0; x < fsi.Length; x++)
            {
                //check to see if this is a temporary storage card (e.g. SD card)
                if ((fsi[x].Attributes & System.IO.FileAttributes.Temporary) == System.IO.FileAttributes.Temporary)
                {
                    //if so, return the path
                    firstCard = fsi[x].FullName;
                }
            }

            return firstCard;
        }

        public static void LogData(string data, string sdCard)
        {
            // write data to the SD card
            try
            {
                StreamWriter sw = new StreamWriter(sdCard + "\\" + Properties.Resources.LogFileName, true);
                sw.Write(data);
                sw.Close();
            }
            catch (Exception e)
            {
                string msg = e.Message;
            }
        }

        public static void Play(string fileName)
        {
            WCE_PlaySound(fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
        }

        public static void Play(Stream stream)
        {
            byte[] soundBytes = new byte[stream.Length];
            stream.Read(soundBytes, 0, (int)stream.Length); 
            WCE_PlaySoundBytes(soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
        }

        // App.config keys
        public static String BMS = "BMS";
        public static String BATTERY_TYPE = "Setup.BatteryType";
        public static String SIMULATION = "Setup.Simulation";
        public static String LOGGING = "Setup.Logging";
        public static String AUTOCONNECT = "Setup.AutoConnect";

        public static NameValueCollection LoadConfig()
        {
            NameValueCollection AppSettings = new NameValueCollection();
            try
            {
                string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
                string ConfigFile = Path.Combine(AppPath, "App.config");

                if (File.Exists(ConfigFile) == false)
                {
                    //System.Windows.Forms.MessageBox.Show("Config file does not exist");
                    //Application.Exit();
                    return AppSettings;
                }

                XmlDocument oXml = new XmlDocument();

                oXml.Load(ConfigFile);

                XmlNodeList oList = oXml.GetElementsByTagName("appSettings");

                AppSettings = new NameValueCollection();

                foreach (XmlNode oNode in oList)
                {
                    foreach (XmlNode oKey in oNode.ChildNodes)
                    {
                        AppSettings.Add(oKey.Attributes["key"].Value, oKey.Attributes["value"].Value);
                    }
                }

                //System.Windows.Forms.MessageBox.Show(ConfigurationSettings.AppSettings["Item1"]);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return AppSettings;
        }

    
    }


}
